<template>
    <div>
      <div class="good-detail">
        <div class="good-detail__image">
          <el-image :src="selectedGoods.pic" fit="contain"></el-image>
        </div>
        <div class="good-detail__info">
          <div class="good-detail__name">{{ selectedGoods.name }}</div>
          <div class="good-detail__price">￥{{ selectedGoods.price }}</div>
          <div class="good-detail__selfwork">
            {{ selectedGoods.selfwork === 1 ? "自营" : "非自营" }}
          </div>
          <el-button type="primary" @click="addToCart">加入购物车</el-button>
        </div>
      </div>
      <div class="comments">
        <el-skeleton style="width: 100%" :loading="loading" animated>
          <template>
            <el-card :body-style="{ padding: '0px', marginBottom: '1px' }">
              <h2 class="comments__title">商品评论</h2>
              <el-rate v-model="comments.avgRate" disabled show-score></el-rate>
              <div class="comments__list">
                <div
                  v-for="comm in comments.commentInfoList"
                  :key="comm.commentDate"
                  class="comments__item"
                >
                  <div class="comments__content">{{ comm.comments }}</div>
                  <div class="comments__info">
                    <div class="comments__user">用户名: {{ comm.userName }}</div>
                    <div class="comments__date">评论日期: {{ comm.commentDate }}</div>
                  </div>
                  <el-rate v-model="comm.rate" disabled></el-rate>
                </div>
              </div>
            </el-card>
          </template>
        </el-skeleton>
      </div>
    </div>
  </template>
  
  <script>
  import { addCartReq } from "@/api/cart";
  import { mapGetters } from "vuex";
  import { listCommentsReq } from "@/api/order";
  
  export default {
    name: "GoodDetail",
    computed: { ...mapGetters(["selectedGoods"]) },
    data() {
      return {
        loading: true,
        comments: {},
      };
    },
    methods: {
      addToCart() {
        addCartReq(this.selectedGoods.id)
          .then(() => {
            this.$message("添加购物车成功");
          })
          .catch();
      },
      listComment() {
        listCommentsReq(this.selectedGoods.id).then((data) => {
          this.comments = data;
          this.loading = false;
        });
      },
    },
    mounted() {
      this.listComment();
    },
  };
  </script>
  
  <style scoped>
  .good-detail {
    display: flex;
    align-items: center;
    margin: 20px;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    height: 400px;
  }
  
  .good-detail__image {
    width: 200px;
    height: 200px;
    margin-right: 20px;
  }
  
  .good-detail__info {
    display: flex;
    flex-direction: column;
  }
  
  .good-detail__name {
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 10px;
    color: #333;
  }
  
  .good-detail__price {
    font-size: 20px;
    color: #f60;
    margin-bottom: 10px;
    font-weight: bold;
  }
  
  .good-detail__selfwork {
    font-size: 16px;
    color: #666;
  }
  
  .comments {
    margin-top: 20px;
    width: 100%;
  }
  
  .comments__title {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  
  .comments__list {
    margin-top: 10px;
  }
  
  .comments__item {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: #f9f9f9;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: background-color 0.3s ease;
    width: 100%;
  }
  
  .comments__item:hover {
    background-color: #eaeaea;
  }
  
  .comments__content {
    margin-bottom: 10px;
  }
  
  .comments__info {
    display: flex;
    justify-content: space-between;
    color: #666;
  }
  
  .comments__user {
    font-size: 14px;
  }
  
  .comments__date {
    font-size: 12px;
  }
  </style>
  